package cn.snorlaxzjt.LBSserver.service;

import cn.snorlaxzjt.LBSserver.po.page.TokenPage;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;

@Slf4j
@Service
public class ImageService
{

    private final String accessKey = "oY-zXtv0GuRK5nq1u4vh2yxqjUQxSre2dGFkyLu8";
    private final String secretKey = "yCFR5qLIokIGNltvb7-6mzIqawNYK_OZ9oymlmi5";
    private final String bucket="zjtzzhang";
    private final long expireSeconds = 600;

    public String uploadImage(MultipartFile file) throws Exception {
        Configuration cfg = new Configuration(Region.huanan());
        UploadManager uploadManager = new UploadManager(cfg);
        String filename=file.getOriginalFilename();
        filename= UUID.randomUUID()+filename.substring(filename.lastIndexOf("."));
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        uploadManager.put(file.getBytes(), filename, upToken);
        return filename;
    }

    public TokenPage getUploadToken(){
        String key = UUID.randomUUID().toString();
        Auth auth = Auth.create(accessKey, secretKey);
        String token = auth.uploadToken(bucket);
        TokenPage page = new TokenPage();
        page.setToken(token);
        page.setKey(key);
        return page;
    }
}
